<template>
  <view>
    <view class="search-box">
      <my-search @click="gotoSearch"></my-search>
    </view>
    <!-- 轮播图 -->
    <swiper :indicator-dots="true" :autoplay="true" :interval="3000" circular :duration="1000">
      <swiper-item v-for="(item, index) in swiperList" :key="index">
        <!-- 页面跳转，并携带商品的ID -->
        <navigator :url="'/subpkg/goods_detail/goods_detail?goods_id=' + item.goods_id" class="swiper-item"><image :src="item.image_src" mode=""></image></navigator>
      </swiper-item>
    </swiper>
    
    <!-- 分类导航 -->
    <view class="nav-list">
      <view @click="navClickHandler(item)" class="nav-item" v-for="(item,index) in navList" :key="index">
        <image :src="item.image_src" class="nav-img" mode=""></image>
      </view>
    </view>
    
    <!-- 楼层信息 -->
    <view class="floor-list">
      <view class="floor-item" v-for="(item,index) in floorList" :key="index">
        <image :src="item.floor_title.image_src" class="floor-title"></image>
        
        <!-- 楼层图 -->
        <view class="floor-img-box">
          <navigator :url="item.product_list[0].url" class="left-img-box">
            <image :src="item.product_list[0].image_src" mode="widthFix" :style="{width:item.product_list[0].image_width + 'rpx'}"></image>
          </navigator>
          
          <view class="right-img-box">
            <navigator :url="sub.url" class="right-img-item" v-for="(sub,i) in item.product_list" :key="i" v-if="i!== 0" >
              <image :src="sub.image_src" :style="{width:sub.image_width + 'rpx'}" mode="widthFix"></image>
            </navigator>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
  import badge from '@/mixins/tabbar-badge.js';
export default {
  data() {
    return {
      swiperList: [],
      navList:[],
      floorList:[]
    };
  },
  mixins: [badge],
  methods: {
    // 获取轮播图数据
    async getswiperList() {
      const { data: res } = await uni.$http.get('/api/public/v1/home/swiperdata');
      // 为数据赋值
      this.swiperList = res.message;
      uni.$showMsg('数据请求成功！')
    },
    gotoSearch(){
      uni.navigateTo({
        url:'/subpkg/search/search'
      })
    },
    
    // 获取navlist的数据
    async getNavList(){
     const {data:res} = await uni.$http.get('/api/public/v1/home/catitems')
     uni.$showMsg('数据请求成功！')
     this.navList = res.message
    },
    
    // 点击分类切换到分类页
    navClickHandler(item){
      if(item.name === '分类') uni.switchTab({
        url:'/pages/cate/cate'
      })
    },
    
    // 获取楼层数据的方法
   async getFloorList(){
      const {data:res} = await uni.$http.get('/api/public/v1/home/floordata')
      
      res.message.forEach(floor => {
          floor.product_list.forEach(prod => {
            prod.url = '/subpkg/goods_list/goods_list?' + prod.navigator_url.split('?')[1]
          })
        })
      this.floorList = res.message
    }
  },
  
  
  onLoad() {
    // 调用方法获取数据
    this.getswiperList();
    this.getNavList()
    this.getFloorList()
  }
};
</script>

<style lang="scss">
swiper {
  height: 330rpx;
  .swiper-item,
  image {
    width: 100%;
    height: 100%;
  }
}

.nav-list{
  display: flex;
  justify-content: space-around;
  margin: 15px 0 ;
  
  .nav-img {
    width: 128rpx;
    height: 140rpx;
  }
}

.floor-title{
  width: 100%;
  height: 60rpx;
}

.floor-img-box{
  display: flex;
  padding-left: 10rpx;
}

.right-img-box{
  display: flex;
  flex-wrap: wrap;
  justify-content: space-around;
}
.search-box{
  position: sticky;
  top: 0;
  z-index: 999;
}
</style>
